//
//  heapManager.cpp
//  DataStructureProject
//
//  Created by zhangbaochuan on 2020/4/9.
//  Copyright © 2020 Geex. All rights reserved.
//

#include "heapManager.hpp"


#pragma mark - MaxHeap

//堆第一个为数组0
void MaxHeap::shiftUp(int k) {
    while (k >= 0) {
        int parent = (k - 1)/2;
        if (data[k] > data[parent]) {
            std::swap(data[k], data[parent]);
            k = parent;
        }else {
            break;
        }
    }
}

#pragma mark - shifDown


MaxHeap:: MaxHeap(int capacity) {
    data = new int[capacity + 1];
    count = 0;
}

MaxHeap:: ~MaxHeap() {
    delete [] data;
}

int MaxHeap::size() {
    return count;
}

bool MaxHeap::isEmpty() {
    return count == 0;
}

void MaxHeap::insert(int v) {
    data[count + 1] = v;
    count ++;
    shiftUp(count);
}

void MaxHeap::testPrint() {
    for (int i = 0; i< count; i++) {
        printf("%d ", data[i]);
    }
}
